package com.comdo.newup_loan.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.comdo.newup_loan.R;
import com.comdo.newup_loan.model.RepayListBean;
import com.comdo.newup_loan.module.repayment.CostListActivity;
import com.comdo.newup_loan.util.StringUtils;

import java.util.ArrayList;

/**
 * Created by liyuan on 2017/9/27.
 */

public class RePayListAdapter extends RecyclerView.Adapter<RePayListAdapter.ViewHolder> implements View.OnClickListener{
    public ArrayList<RepayListBean> datas = null;
    private OnItemClickListener mOnItemClickListener = null;
    private Context context;
    public RePayListAdapter(ArrayList<RepayListBean> datas, Context context){
        this.datas=datas;
        this.context=context;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        viewHolder.itemView.setTag(position);
        viewHolder.mTitleText.setText(datas.get(position).getTitle());
        viewHolder.mAmountText.setText(StringUtils.getMoneyString(datas.get(position).getBorrowAmount()));
        viewHolder.mCountText.setText(datas.get(position).getiCount()+"");

        viewHolder.mDatePayText.setText(datas.get(position).getCreateTime());
        if(datas.get(position).getLoanAgain()==1){
            viewHolder.mXdImageView.setVisibility(View.VISIBLE);
            viewHolder.mCostListText.setVisibility(View.GONE);
        }else{
            viewHolder.mXdImageView.setVisibility(View.GONE);
            viewHolder.mCostListText.setVisibility(View.VISIBLE);
        }
        if(datas.get(position).getState()==5||datas.get(position).getState()==14){
            viewHolder.mTagText.setText("期，已");
            viewHolder.mStateText.setText("结清");
            viewHolder.mhasPayText.setText("");
        }else if(datas.get(position).getState()==10||datas.get(position).getState()==16){
            viewHolder.mTagText.setText("期，已");
            viewHolder.mhasPayText.setText("");
            viewHolder.mStateText.setText("逾期");
        }else {
            viewHolder.mTagText.setText("期，已还");
            viewHolder.mhasPayText.setText(datas.get(position).getActualBackCount()+"");
            viewHolder.mStateText.setText("期");
        }
        viewHolder.mCostListText.setId(datas.get(position).getId());
        viewHolder.mCostListText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, CostListActivity.class);
                intent.putExtra("tenderId",v.getId());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_repayment_list,parent,false);
        ViewHolder vh = new ViewHolder(view);
        view.setOnClickListener(this);
        return vh;
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }
    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mTitleText;
        public TextView mAmountText;
        public TextView mCountText;
        public TextView mhasPayText;
        public TextView mDatePayText;
        public TextView mCostListText;
        public TextView mStateText;
        public TextView mTagText;
        public ImageView mXdImageView;
        public ViewHolder(View view){
            super(view);
            mTitleText = (TextView) view.findViewById(R.id.tv_title);
            mAmountText = (TextView) view.findViewById(R.id.tv_amount);
            mCountText = (TextView) view.findViewById(R.id.tv_count);
            mhasPayText = (TextView) view.findViewById(R.id.tv_has_pay);
            mDatePayText = (TextView) view.findViewById(R.id.tv_date);
            mCostListText = (TextView) view.findViewById(R.id.tv_cost_list);
            mStateText = (TextView) view.findViewById(R.id.tv_state);
            mTagText=(TextView) view.findViewById(R.id.tv_tag);
            mXdImageView=(ImageView)view.findViewById(R.id.img_xd);
        }
    }
    public static interface OnItemClickListener {
        void onItemClick(View view , int position);
    }
}
